package kata09.customer;

import kata09.info.Goods;
import kata09.order.Order;
import lombok.Data;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

//购物车
@Data
public class ShoppingCar {

    //商品购物存储
    private Map<String,BuyGoods> car;

    public ShoppingCar(){
        car = new HashMap<>();
    }
    //添加商品到购物车
    public void addShoppingCar(Goods goods,Integer number){
        //获取商品总数
        BuyGoods buyGoods = car.get(goods.getGoods().getName());
        if(buyGoods == null){
            //新建 单个商品购买类
            buyGoods = new BuyGoods(goods,number);
            car.put(goods.getGoods().getName(),buyGoods);
        }else {
            buyGoods.setBuyNumber((buyGoods.getBuyNumber() +number));
            car.put(goods.getGoods().getName(),buyGoods);
        }
    }

    //生成订单
    public Order generateOrder(){
        List<BuyGoods> buyGoods = new ArrayList<>();
        for(Map.Entry<String,BuyGoods> entry : car.entrySet()){
            buyGoods.add(entry.getValue());
        }
        Order order = new Order();
        order.setBuyGoods(buyGoods);
        return order;
    }
}
